package com.bi.bitest.bao.fragments;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bi.bitest.bao.R;
import com.bi.bitest.bao.adapters.CommonFragmentPagerAdapter;
import com.bi.bitest.bao.fragments.discover.*;

import java.util.LinkedList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverFragment extends Fragment /*implements TabLayout.OnTabSelectedListener*/ {
    private ViewPager pager;
    private List<BaseFragment>fragments;
    public DiscoverFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret=inflater.inflate(R.layout.fragment_discover,container,false);
        TabLayout tabLayout= (TabLayout) ret.findViewById(R.id.discover_tab_bar);
       /* TabLayout.Tab tab=tabLayout.newTab().setText("推荐");
        tabLayout.addTab(tab);
        tab=tabLayout.newTab().setText("分类");
        tabLayout.addTab(tab);
         tab=tabLayout.newTab().setText("直播");
        tabLayout.addTab(tab);
       tab=tabLayout.newTab().setText("榜单");
        tabLayout.addTab(tab);
      tab=tabLayout.newTab().setText("主播");
        tabLayout.addTab(tab);*/



          pager= (ViewPager) ret.findViewById(R.id.discover_viewpager);
       fragments=new LinkedList<BaseFragment>();
      fragments.add(new DiscoverRecommendFragment());
        fragments.add(new DiscoverCategoryFragment());
        fragments.add(new DiscoverLiveFragment());
        fragments.add(new DiscoverRatingFragment());
        fragments.add(new DiscoverAnchorFragment());

        CommonFragmentPagerAdapter pagerAdapter=new CommonFragmentPagerAdapter(
                getChildFragmentManager(),fragments);

        pager.setAdapter(pagerAdapter);
/*       pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setOnTabSelectedListener(this);*/
        //此方法将以上两行代替并且也会设置每个tab的title
   tabLayout.setupWithViewPager(pager);
    return ret;
    }


   /* @Override
    public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition(),true);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }*/
}
